﻿Imports System.IO
Imports System.Xml

Public NotInheritable Class UsedExpressionCollection

    Private Shared __FilePath As String = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "UsedExpressions.xml")
    Private Shared __List As List(Of UsedExpression) = New List(Of UsedExpression)()
    Private Shared __SampleExpressionsTexts() As String = {
        "1+1",
        "DateTimeAdd(#2009-12-15 10:48:59#,30,""d"")",
        "Year(#2009-12-15 10:48:59#)",
        "Not True",
        "5*3^2",
        "4 % 3",
        """I"" + "" was "" + ""concatenated!""",
        "Contains(""Expressions"",""x"")",
        "IndexOf(""aeiou"",""a"")",
        "IsBoolean(""true"")",
        "18=9*2 and 99=100-1"
        }

    Private Sub New()
    End Sub

    Public Shared Sub Initialize()
        Dim _expressions As List(Of UsedExpression) = __ReadFromFile()
        If _expressions.Count = 0 Then
            For Each _sampleExpressionText As String In __SampleExpressionsTexts
                Dim _compiledExpression As Enhancements.Basic.Expressions.Expression = Application.Compiler.Compile(_sampleExpressionText)
                Dim _result As Object = _compiledExpression.Solve()
                __List.Add(New UsedExpression(_compiledExpression, _result, Nothing))
            Next _sampleExpressionText
            SaveToFile()
        Else
            __List.AddRange(_expressions)
        End If
    End Sub

    Private Shared Function __ReadFromFile() As List(Of UsedExpression)
        Dim _expressions As List(Of UsedExpression) = New List(Of UsedExpression)()

        Try
            Dim _xDocument As XDocument = XDocument.Load(__FilePath)
            For Each _xExpression As XElement In _xDocument...<Expression>
                _expressions.Add(New UsedExpression(_xExpression))
            Next _xExpression

        Catch
        End Try

        Return _expressions
    End Function

    Public Shared Sub SaveToFile()
        Dim _xDocument As XDocument = New XDocument()

        Dim _xElementExpressions As XElement = New XElement(XName.Get("Expressions"))
        For Each _expression As UsedExpression In __List
            _xElementExpressions.Add(_expression.ToXElement())
        Next (_expression)
        _xDocument.Add(_xElementExpressions)

        Try
            _xDocument.Save(__FilePath)

        Catch
        End Try
    End Sub

    Public Shared Sub Append(ByVal _text As String, ByVal _resultValue As Object, ByVal _resultException As Exception)
        If _resultException Is Nothing Then
            __List.Add(New UsedExpression(_text, _resultValue, Nothing))
        Else
            __List.Add(New UsedExpression(_text, _resultValue, _resultException.GetType().Name))
        End If
        SaveToFile()
    End Sub

    Public Shared Sub Append(ByVal _expression As Enhancements.Basic.Expressions.Expression, ByVal _resultValue As Object, ByVal _resultException As Exception)
        If _resultException Is Nothing Then
            __List.Add(New UsedExpression(_expression, _resultValue, Nothing))
        Else
            __List.Add(New UsedExpression(_expression, _resultValue, _resultException.GetType().Name))
        End If
        SaveToFile()
    End Sub

    Public Shared ReadOnly Property Count As Integer
        Get
            Return __List.Count
        End Get
    End Property

    Public Shared ReadOnly Property Item(ByVal _index As Integer) As UsedExpression
        Get
            Return __List(_index)
        End Get
    End Property

End Class
